﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;


namespace HZ.DrugTrade.Web.Admin.BidProdManage
{
    public partial class BidProductUAddNmg : BasePage
    {
        public BidProductUAddNmg()
        {
            //设置页面 模块以及操作类型
            this.ModuleNo = Module.采购产品管理.ToString();
            this.PageOperateType = OperateType.编辑.ToString();
        }

        protected HZ.DrugTrade.BLL.BLL_Common bll = new HZ.DrugTrade.BLL.BLL_Common();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="goodsModel"></param>
        /// <returns></returns>
        private decimal GetLimitPrice(HZ.DrugTrade.Model.Goods goodsModel)
        {

            if (goodsModel.BidPrice != null)
            {
                if (goodsModel.BidPrice.Value <= 500)
                {
                    return goodsModel.BidPrice.Value * (decimal)1.15;
                }
                else
                {
                    return goodsModel.BidPrice.Value + 75;
                }
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 编辑采购产品
        /// </summary>
        private void AddBidProduct()
        {
            //判断流水号是否为空
            if (string.IsNullOrEmpty(txtGoodsId.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("产品编号不能为空！", this);
                txtGoodsId.Focus();
                return;
            }
            //判断采购价格是否为空
            if (string.IsNullOrEmpty(txtProcurePrice.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("采购价格不能为空！", this);
                txtProcurePrice.Focus();
                return;
            }
            //获取新增采购产品的相关信息
            Goods goods = new Goods();

            if (!string.IsNullOrEmpty(txtGoodsId.Text.Trim()))
            {
                goods.GoodsID = txtGoodsId.Text.Trim();

                if (txtProcurePrice.Text.Trim() != "")
                {
                    goods.BidPrice = Convert.ToDecimal(txtProcurePrice.Text.Trim());
                    goods.LimitPrice = GetLimitPrice(goods);
                }
                goods.GoodsName = this.txtGoodsName.Value;
                //goods.Factor = Convert.ToInt16(this.txtFactor.Text.Trim());
                goods.Material = this.txtMaterial.Text;
                goods.Outlookc = this.txtOutlookc.Text;
                //goods.Pack = this.txtOutlookc.Text + "*" + this.txtFactor.Text.Trim();
                goods.Unit = this.txtUnit.Text;
                goods.PackUnit = this.txtPackUnit.Text;
                //goods.Source = Convert.ToInt16(this.ddlSortSource.SelectedValue);

                goods.CompanyID_SC = this.txtCompanyID_SC.Text.Trim().ToUpper();
                goods.CompanyName_SC = this.txtCompany_SC.Text.Trim();
                goods.CompanyID_TB = this.txtCompanyID_TB.Text.Trim().ToUpper();
                goods.CompanyName_TB = this.txtCompany_TB.Text.Trim();
                goods.QualityLevel = dplLevel.SelectedItem.Value;
                goods.SortID = "0000";
                //使用方法，执行操作，采购产品
                IBLL_Goods ipb = Factory.BLL_Goods();
                int flag = ipb.AddGoods(goods, CurrentAccount.UserID);
                if (flag == 1)
                {
                    AddProcurecatalog();  //增加采购目录 add by zdc 升级后view_procurecatalog_tb
                    PageInfo("操作成功！", "BidProductList.aspx");
                }
                else if (flag == 0)
                {
                    PageError("操作失败！", this.ReturnUrl);
                }
                else if (flag == 2)
                {
                    PageError("产品编号重复！", this.ReturnUrl);
                }
                else if (flag == 3)
                {
                    PageError("生产企业编号不存在，但生产企业用户名已存在！", this.ReturnUrl);
                }
                else if (flag == 4)
                {
                    PageError("投标企业编号不存在，但投标企业用户名已存在！", this.ReturnUrl);
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("未选择任何产品！", this);
            }
        }

        /// <summary>
        /// 增加采购目录 add by zdc 升级后view_procurecatalog_tb
        /// </summary>
        private void AddProcurecatalog()
        {

            //获取新增采购产品的相关信息
            View_Procurecatalog_TB vpt = new View_Procurecatalog_TB();

            if (!string.IsNullOrEmpty(txtGoodsId.Text.Trim()))
            {
                vpt.GoodsID = txtGoodsId.Text.Trim();
                vpt.ProcureCatalogID=txtGoodsId.Text.Trim();
                vpt.IsJiCeng = 0;//0允许 1不允许 add by zdc
                vpt.GoodsName = this.txtGoodsName.Value;
                //goods.ShortName = this.txtShortName.Value;
                vpt.ProcureStartTime = DateTime.Parse("2011-1-1 10:06:09");
                vpt.ProcureEndTime = DateTime.Parse("2011-12-31 10:06:09");
                //vpt.Factor = Convert.ToInt16(this.txtFactor.Text.Trim());
                vpt.Material = this.txtMaterial.Text;
                vpt.Outlookc = this.txtOutlookc.Text;
                //vpt.Pack = this.txtOutlookc.Text + "*" + this.txtFactor.Text.Trim();
                vpt.Unit = this.txtUnit.Text;
                vpt.PackUnit = this.txtPackUnit.Text;
                //vpt.SortSource = Convert.ToInt16(this.ddlSortSource.SelectedValue);
                vpt.AddTime=DateTime.Now;
                vpt.CompanyID_SC = this.txtCompanyID_SC.Text.Trim().ToUpper();
                vpt.CompanyName_SC = this.txtCompany_SC.Text.Trim();
                vpt.CompanyID_TB = this.txtCompanyID_TB.Text.Trim().ToUpper();
                vpt.CompanyName_TB = this.txtCompany_TB.Text.Trim();
                if (txtProcurePrice.Text.Trim() != "")
                {
                    vpt.ProcurePrice = Convert.ToDecimal(txtProcurePrice.Text.Trim());
                    
                }
                //vpt.ProjectName = "内蒙古网上产品采购项目2011";
                vpt.IsUsing = true;
                vpt.IsFlowing = false;
                vpt.ProjectID = "0001";
                vpt.SortID="0000";
               // vpt.Qualitylevel = "0000";
                //使用方法，执行操作，采购产品
                IBLL_Goods ipb = Factory.BLL_Goods();
                int flag = ipb.AddVPT(vpt, CurrentAccount.UserID);
                if (flag == 1)
                {
                    PageInfo("操作成功！", "BidProductList.aspx");
                }
                else if (flag == 0)
                {
                    PageError("操作失败！", this.ReturnUrl);
                }
            }
            else
            {
                HZ.Web.WebUtil.Alert("未选择任何产品！", this);
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                AddBidProduct();
            }
        }

    }
}

